package com.kunbo.cn.service.impl.core.purchase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.purchase.SalesVolumeHistoryDao;
import com.kunbo.cn.dto.SalesVolumeHistoryDto;
import com.kunbo.cn.entity.core.purchase.SalesVolumeHistory;
import com.kunbo.cn.service.core.purchase.SalesVolumeHistoryService;

@Service
public class SalesVolumeHistoryServiceImpl implements SalesVolumeHistoryService{

	@Autowired
	SalesVolumeHistoryDao dao;
	
	@Autowired
	private JdbcTemplate jdbc;
	
	@Override
	public boolean save(String uuid, String create_user_id, Integer taskid, String order_number, String adjustment_time,
			double quantity_before_adjustment, double adjusted_quantity, String Adjusts) {
		// TODO Auto-generated method stub
		SalesVolumeHistory volumeHistory = new SalesVolumeHistory();
		volumeHistory.setUuid(uuid);
		volumeHistory.setCreate_user_id(create_user_id);
		volumeHistory.setTaskid(taskid);
		volumeHistory.setOrder_number(order_number);
		volumeHistory.setAdjustment_time(adjustment_time);
		volumeHistory.setQuantity_before_adjustment(quantity_before_adjustment);
		volumeHistory.setAdjusted_quantity(adjusted_quantity);
		volumeHistory.setAdjusts(Adjusts);
		volumeHistory = dao.save(volumeHistory);
		if (volumeHistory != null)
			return false;
		else
			return true;
	}

	@Override
	public List<SalesVolumeHistoryDto> get(String order_number) {
		// TODO Auto-generated method stub
		List<SalesVolumeHistoryDto> volumeHistory = new ArrayList<SalesVolumeHistoryDto>();
		String sql = "select * from sales_volume_history where order_number = '" + order_number + "'";
		List<Map<String, Object>>  list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			SalesVolumeHistoryDto dto = new SalesVolumeHistoryDto();
			sql = "select * from sys_user where id = '" + map.get("create_user_id").toString() + "'";
			Map<String, Object> user = jdbc.queryForMap(sql);
			dto.setAdjustment(map);
			dto.setUser(user);
			volumeHistory.add(dto);
		}
		return volumeHistory;
		
	}
	
}
